package com.photoselector.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;

import com.photoselector.R;
import com.photoselector.view.PhotoItem;
import com.photoselector.listener.PhotoItemClickListener;
import com.photoselector.listener.PhotoItemListener;
import com.photoselector.listener.OnPhotoItemCheckedListener;
import com.photoselector.model.PhotoModel;
import com.photoselector.ui.MBaseAdapter;

import java.util.ArrayList;

/**
 * @author Aizaz AZ
 */


public class PhotoSelectorAdapter extends MBaseAdapter<PhotoModel> {
    private static final String LOG_TAG = PhotoSelectorAdapter.class.getSimpleName();
    private int itemWidth;
    private int horizentalNum = 3;
    private OnPhotoItemCheckedListener listener;
    private LayoutParams itemLayoutParams;
    private PhotoItemClickListener mCallback;
    private OnClickListener cameraListener;
    private PhotoItemListener photoItemListener;

    private PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models) {
        super(context, models);
    }

    public PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models, int screenWidth, OnPhotoItemCheckedListener listener, PhotoItemClickListener mCallback,
                                OnClickListener cameraListener) {
        this(context, models);
        setItemWidth(screenWidth);
        this.listener = listener;
        this.mCallback = mCallback;
        this.cameraListener = cameraListener;
    }

    public void setPhotoItemListener(PhotoItemListener listener) {
        this.photoItemListener = listener;
    }


    /**
     * 设置每一个Item的宽高
     */
    public void setItemWidth(int screenWidth) {
        int horizentalSpace = context.getResources().getDimensionPixelSize(R.dimen.sticky_item_horizontalSpacing);
        this.itemWidth = (screenWidth - (horizentalSpace * (horizentalNum - 1))) / horizentalNum;
        this.itemLayoutParams = new LayoutParams(itemWidth, itemWidth);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoItem item = null;
        if (convertView == null || !(convertView instanceof PhotoItem)) {
            item = new PhotoItem(context, listener);
            item.setLayoutParams(itemLayoutParams);
            convertView = item;
        } else {
            item = (PhotoItem) convertView;
        }
        item.setPhotoItemListener(photoItemListener);
        item.setImageDrawable(models.get(position));
        item.setSelected(models.get(position).isChecked());
        item.setOnClickListener(mCallback, position);
        return convertView;
    }
}
